package cave.renderer;

import android.util.Log;
import cave.utils.FloatArray;
import cave.utils.IntArray;

public class RenderList {
	
	public static final int ELEM_X = 0;
	public static final int ELEM_Y = 1;
	public static final int ELEM_WIDTH = 2;
	public static final int ELEM_HEIGHT = 3;
	public static final int ELEM_DIRX = 4;
	public static final int ELEM_DIRY = 5;
	public static final int ELEM_U = 6;
	public static final int ELEM_V = 7;
	public static final int ELEM_UVWIDTH = 8;
	public static final int ELEM_UVHEIGHT = 9;
	public static final int ELEM_TEXID = 10;
	public static final int ELEM_RCOLOR = 11;
	public static final int ELEM_GCOLOR = 12;
	public static final int ELEM_BCOLOR = 13;
	public static final int ELEM_ACOLOR = 14;
	public static final int NUM_ELEMENTS = 15;
	
	public static final int WORLD_ID = 0;
	public static final int SCREEN_ID = 1;
	
	private FloatArray spriteList;
	private IntArray listInfo;
	private int lastType, lastTypeCount;
	
	public RenderList() {
		spriteList = new FloatArray(200 * NUM_ELEMENTS);
		listInfo = new IntArray(100);		
	}

	public void addWorldSprite(Sprite sprite) {
		addSprite(sprite);
		
		if (lastType != WORLD_ID) {
			listInfo.add((SCREEN_ID << 16) | lastTypeCount);
			lastType = WORLD_ID;
			lastTypeCount = 1;
		} else {
			lastTypeCount++;
		}
	}
	
	public void addScreenSprite(Sprite sprite) {
		addSprite(sprite);
		
		if (lastType != SCREEN_ID) {
			listInfo.add((WORLD_ID << 16) | lastTypeCount);
			lastType = SCREEN_ID;
			lastTypeCount = 1;
		} else {
			lastTypeCount++;
		}
	}
	
	public void addScreenData(float x, float y, float width, float height, 
			float dirX, float dirY, Texture tex,
			float r, float g, float b, float a) {
		
		spriteList.add(x);
		spriteList.add(y);
		spriteList.add(width);
		spriteList.add(height);
		spriteList.add(dirX);
		spriteList.add(dirY);

		spriteList.add(tex.getLeft());
		spriteList.add(tex.getBottom());
		spriteList.add(tex.getWidth());
		spriteList.add(tex.getHeight());
		spriteList.add(Float.intBitsToFloat(tex.getAtlas().getId()));
		
		spriteList.add(r);
		spriteList.add(g);
		spriteList.add(b);
		spriteList.add(a);
		
		if (lastType != SCREEN_ID) {
			listInfo.add((WORLD_ID << 16) | lastTypeCount);
			lastType = SCREEN_ID;
			lastTypeCount = 1;
		} else {
			lastTypeCount++;
		}
		
	}
	
	public void ready() {
		listInfo.add((lastType << 16) | lastTypeCount);
	}
	
	public void clear() {
		spriteList.clear();
		listInfo.clear();
		lastType = WORLD_ID;
		lastTypeCount = 0;
	}
	
	public FloatArray getSprites() {
		return spriteList;
	}

	
	public IntArray getListInfo() {
		return listInfo;
	}
	
	private void addSprite(Sprite sprite) {
		spriteList.add(sprite.getX());
		spriteList.add(sprite.getY());
		spriteList.add(sprite.getWidth());
		spriteList.add(sprite.getHeight());
		spriteList.add(sprite.getOrientation()[0]);
		spriteList.add(sprite.getOrientation()[1]);

		Texture tex = sprite.getTexture();
		spriteList.add(tex.getLeft());
		spriteList.add(tex.getBottom());
		spriteList.add(tex.getWidth());
		spriteList.add(tex.getHeight());
		spriteList.add(Float.intBitsToFloat(tex.getAtlas().getId()));
		
		spriteList.add(sprite.getColor()[0]);
		spriteList.add(sprite.getColor()[1]);
		spriteList.add(sprite.getColor()[2]);
		spriteList.add(sprite.getColor()[3]);
	}
	
}
